import 'package:backend.core/core/domain/entities/web/yookassa/yookassa_payment_method.dart';
import 'package:backend.purchases/domain/entities/yookassa_api/payments/yookassa_api_succeeded_payment_method.dart';
import 'package:backend.purchases/domain/helpers/yookassa/create_yookassa_bank_card_info_from_api_one.dart';

abstract class UpdateYookassaPaymentMethodFromApiOne {
  YookassaPaymentMethod call({
    required YookassaPaymentMethod paymentMethod,
    required YookassaApiSucceededPaymentMethod apiPaymentMethod,
  });
}

class UpdateYookassaPaymentMethodFromApiOneImpl implements UpdateYookassaPaymentMethodFromApiOne {
  const UpdateYookassaPaymentMethodFromApiOneImpl({
    required CreateYookassaBankCardInfoFromApiOne createYookassaBankCardInfoFromApiOne,
  }) : _createYookassaBankCardInfoFromApiOne = createYookassaBankCardInfoFromApiOne;

  final CreateYookassaBankCardInfoFromApiOne _createYookassaBankCardInfoFromApiOne;

  @override
  YookassaPaymentMethod call({
    required YookassaPaymentMethod paymentMethod,
    required YookassaApiSucceededPaymentMethod apiPaymentMethod,
  }) {
    final bankCardInfo = _createYookassaBankCardInfoFromApiOne(
      apiBankCardInfo: apiPaymentMethod.bankCardInfo,
    );

    return paymentMethod.copyWith(
      saved: () => paymentMethod.saved && apiPaymentMethod.saved,
      bankCardInfo: () => bankCardInfo,
    );
  }
}
